/*
 * Copyright (C) 2012 Rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.server.packets;

import rpg.server.net.NetworkHandler;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;

/**
 * rothens.tumblr.com
 * @author Rothens
 */
public abstract class Packet {
	
	private static HashMap<Integer, Class> classes = new HashMap<Integer, Class>();
	private static HashMap<Class, Integer> ids = new HashMap<Class, Integer>();
	private static HashSet<Integer> clientPackets = new HashSet<Integer>();
	private static HashSet<Integer> serverPackets = new HashSet<Integer>();
	public final long timeCreated = System.currentTimeMillis();
	
	public Packet() {
		
	}
	
	static void createMapping(int i, boolean client, boolean server, Class clazz){
		if(classes.containsKey(i)) {
			throw new IllegalArgumentException("Duplicate id entry: " + i);
		}
		
		if(ids.containsKey(clazz)){
			throw new IllegalArgumentException("Duplicate class: " + clazz.getSimpleName());
		}
		
		ids.put(clazz, i);
		classes.put(i, clazz);
		if(client){
			clientPackets.add(i);
		}
		if(server) {
			serverPackets.add(i);
		}
	}
	
	public static Packet getNewPacket(int i){
		try {
			Class c = classes.get(i);
			if(c == null){
				return null;
			} else {
				return (Packet) c.newInstance();
			}
		} catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public final int getPacketId() {
		return ids.get(getClass()).intValue();
	}
	
	public static Packet readPacket(DataInputStream stream, boolean server) throws IOException {
		int i = 0;
		Packet packet = null;
		try {
			i = stream.read();
			if (i == -1) {
				return null;
			}
			if(server && !serverPackets.contains(i) || !server && !clientPackets.contains(i)) {
				throw new IOException("Wrong id: " + i);
			}
			packet = getNewPacket(i);
			if(packet == null) {
				throw new IOException("Wrong id: " + i);
			}
			packet.readPacketData(stream);
		} catch (EOFException e){
			System.out.println("End of stream");
			return null;
		}
		return packet;
	}
	
	public static void writePacket(Packet packet, DataOutputStream dos) throws IOException {
		dos.write(packet.getPacketId());
		packet.writePacketData(dos);
	}
	
	public static void writeString(String s, DataOutputStream dos) throws IOException {
		if(s.length() > 65535){
			throw new IOException("String too big!");
		} else {
			dos.writeShort(s.length());
			dos.writeChars(s);
			return;
		}
	}
	
	public static String readString(DataInputStream dis, int i) throws IOException{
		short len = dis.readShort();
		if(len > i) {
			throw new IOException("String bigger then allowed!");
		}
		if(len <0) {
			throw new IOException("LoLWut! String length.. LESS than zero! o_O");
		}
		String ret = "";
		for(int j = 0; j < len; j++){
			ret += dis.readChar();
		}
		return ret;
	}
	
	public abstract void readPacketData(DataInputStream dis) throws IOException;
	
	public abstract void writePacketData(DataOutputStream dos) throws IOException;
	
	public abstract int getSize();
	
	public abstract void processPacketData(NetworkHandler h);
	
	static Class getClass(String name){
		try {
			return Class.forName(name);
		} catch (ClassNotFoundException e){
			throw new NoClassDefFoundError(e.getMessage());
		}
	}
	
	//Register packets here
	static {
		
	}
	
}
